package com.example.config;

import com.example.entity.User;
import com.example.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.env.OriginTrackedMapPropertySource;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;

import javax.annotation.Resource;
import java.util.Map;

@Configuration
public class EnvironmentConfig implements EnvironmentAware {

    @Resource
    UserMapper userMapper;

    @Override
    public void setEnvironment(Environment environment) {
        ConfigurableEnvironment environmentTemp=(ConfigurableEnvironment)environment;
        MutablePropertySources propertySources = environmentTemp.getPropertySources();
        for (PropertySource<?> propertySource : propertySources) {
            if (propertySource instanceof OriginTrackedMapPropertySource){
                OriginTrackedMapPropertySource om=(OriginTrackedMapPropertySource)propertySource;
                Map<String, Object> source = om.getSource();
                User user = userMapper.selectById("123456");
                source.put("spring.mail.username",user.getName());
            }
        }
    }
}